package action.contract;

import java.util.ArrayList;
import java.util.List;

import model.TContract;
import model.contract.Loangrant;
import model.credit.Creditapplyenterprise;
import model.credit.Creditapplypersonal;
import model.grant.Customgrant;
import service.contract.ContractService;
import service.contract.ShowContractService;
import service.grant.GrantService;
import util.Sessions;
import util.Time;
import util.Tool;
import core.action.BaseAction;
import core.global.Global;
import exception.Exceptions;

public class ContractNewAction extends BaseAction{
	
	private String contractY;
	private String contractN;
	private TContract contract;
	private String contracttype;
	private String loangranttype;
	private String granttime;
	private List<?> contractensure;
	private ContractService contractService;
	private ShowContractService showContractService;
	private GrantService grantService;
	private String grantdate;
	private String grantmoney;
	private String refunddate;
	
	public String execute() throws Exceptions{
		Integer creditid=(Integer)Sessions.get("creditid");
		List<TContract> idList=showContractService.get_htList();
		//---向TContract表中存数据---//
		String contractId;
		if(contract.getContracttype().equals("1"))
		{
			contracttype="个借";
			contract.setClienttype("1");
		}else if(contract.getContracttype().equals("2"))
		{
			contracttype="企借";
			contract.setClienttype("2");
		}
        contractId=contractY+"年"+contracttype+"字第"+contractN+"号";
        if(idList != null){
        	for(TContract c:idList){
            	if(c.getContractId().equals(contractId)){
            		MessageJump("重复的合同编号！","BACK");
            		return "message";
            	}
            }
        }
        contract.setContractId(contractId);
        contract.setBalance(contract.getApplymoney());
        contract.setContractstate(10);
        if(loangranttype.equals("1")){
        	contract.setLoangrant("1");
        }else if(loangranttype.equals("n")){
        	contract.setLoangrant(granttime);
        }
        contract.setCreatetime(Time.CurrentTimestamp());
        contract.setClerkid(Global.userid().intValue());
        contract.setApplyid(creditid);
        if(contract.getClienttype().equals("1")){
        	Creditapplypersonal creditp=contractService.get_creditpersonal(creditid);
        	contract.setIsaboutn(creditp.getIsAboutN());
        	if(creditp.getModeB()==null){
        		contract.setModeB(0);
        	}else{
        		contract.setModeB(creditp.getModeB());
        	}
        	if(creditp.getModeD()==null){
        		contract.setModeD(0);
        	}else{
        		contract.setModeD(creditp.getModeD());
        	}
        	if(creditp.getModeX()==null){
        		contract.setModeX(0);
        	}else{
        		contract.setModeX(creditp.getModeX());
        	}
        	if(creditp.getModeZ()==null){
        		contract.setModeZ(0);
        	}else{
        		contract.setModeZ(creditp.getModeZ());
        	}
        }else if(contract.getClienttype().equals("2")){
        	Creditapplyenterprise creditc=contractService.get_creditcompany(creditid);
        	contract.setIsaboutn(creditc.getIsAboutN());
        	if(creditc.getModeB()==null){
        		contract.setModeB(0);
        	}else{
        		contract.setModeB(creditc.getModeB());
        	}
        	if(creditc.getModeD()==null){
        		contract.setModeD(0);
        	}else{
        		contract.setModeD(creditc.getModeD());
        	}
        	if(creditc.getModeX()==null){
        		contract.setModeX(0);
        	}else{
        		contract.setModeX(creditc.getModeX());
        	}
        	if(creditc.getModeZ()==null){
        		contract.setModeZ(0);
        	}else{
        		contract.setModeZ(creditc.getModeZ());
        	}
        }
        if(contract.getContractdate().after(contract.getCreatetime())||contract.getContractdate().after(contract.getStartdate())||contract.getContractdate().after(contract.getEnddate())){
        	MessageJump("合同签订日期填写有误！","BACK");
        	return "message";
        }
		int contractkey=contractService.save_in_contract(contract);
		//---向loangrant中存数据---//
		if(contract.getLoangrant().equals("1")){
			Loangrant loangrant=new Loangrant();
			loangrant.setContractid(contractkey);
			loangrant.setGrantnum(1);
			loangrant.setGrantdate(contract.getStartdate());
			loangrant.setRefunddate(contract.getEnddate());
			loangrant.setGrantmoney(contract.getApplymoney());
			loangrant.setBalance(contract.getApplymoney());
			loangrant.setState(10);
			contractService.save_in_loangrant(loangrant);
		}else{
		String[] date=Tool.splitArrayHtml(grantdate);
		String[] money=Tool.splitArrayHtml(grantmoney);
		String[] refund=Tool.splitArrayHtml(refunddate);
		Double Countmoney=0.0;
		for(int i=0;i<money.length;i++){
			Countmoney+=Double.valueOf(money[i]);
		}
		if(Countmoney<contract.getApplymoney()){
			MessageJump("所发放的每笔贷款之和不足"+contract.getApplymoney(),"BACK");
			return "message";
		}
		if(Countmoney>contract.getApplymoney()){
			MessageJump("所发放的每笔贷款之和超过"+contract.getApplymoney(),"BACK");
			return "message";
		}
		List<Loangrant> loangrantlist=new ArrayList<Loangrant>();
		for(int i=0;i<date.length;i++){
			Integer n=i+1;
			Loangrant loangrant=new Loangrant();
			loangrant.setGrantnum(n);
			if(Time.DateString2Timestamp(date[i]).before(contract.getStartdate())){
				MessageJump("第"+n+"笔贷款的发放日期不再贷款期限内","BACK");
				return "message";
			}
			loangrant.setGrantdate(Time.DateString2Timestamp(date[i]));
			if(Time.DateString2Timestamp(refund[i]).after(contract.getEnddate())){
				MessageJump("第"+n+"笔贷款的到期日期不再贷款期限内","BACK");
				return "message";
			}
			loangrant.setRefunddate(Time.DateString2Timestamp(refund[i]));
			loangrant.setGrantmoney(Double.valueOf(money[i]));
			loangrant.setBalance(Double.valueOf(money[i]));
			loangrant.setContractid(contractkey);
			loangrant.setState(10);
			loangrantlist.add(loangrant);
		}
		for(Loangrant lg:loangrantlist){
			contractService.save_in_loangrant(lg);
		}
		}
		//Sessions.set("contractloanid",contract.getContractId());
		//---修改状态---//
		if(contract.getContracttype().equals("1")){
			Creditapplypersonal creditpersonal=contractService.get_creditpersonal(creditid);
			creditpersonal.setCreditstate(0);
			contractService.save_in_creditPersonal(creditpersonal);
		}
		if(contract.getContracttype().equals("2")){
			Creditapplyenterprise creditcompany=contractService.get_creditcompany(creditid);
			creditcompany.setCreditstate(0);
			contractService.save_in_creditCompany(creditcompany);
		}
		//-------修改已用授信金额-------//
		Customgrant grant=grantService.getGrant("customid",contract.getClientid());
		Double a=grant.getUsedmoney();
		grant.setUsedmoney(a+contract.getApplymoney());
		grantService.save_in_customgrant(grant);
		
		//----下一步操作------//
if(contractService.get_countrelation(creditid,1)!=0){
	MessageJump("借款合同已保存，接下来将生成抵押合同","contractRelation!getMortgageList");
	}else if(contractService.get_countrelation(creditid, 2)!=0){
	MessageJump("借款合同已保存，接下来将生成质押合同","contractRelation!getPledgeList");
	}else if(contractService.get_countrelation(creditid, 3)!=0){
		MessageJump("借款合同已保存，接下来将生成保证合同","contractEnsureListAction!getList");
	}else{
			MessageJump("操作已完成","showContract!showcheck");
		}
		return "message";
	}

	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}


	public String getContractY() {
		return contractY;
	}


	public void setContractY(String contractY) {
		this.contractY = contractY;
	}


	public String getContractN() {
		return contractN;
	}


	public void setContractN(String contractN) {
		this.contractN = contractN;
	}


	public TContract getContract() {
		return contract;
	}


	public void setContract(TContract contract) {
		this.contract = contract;
	}

	public String getContracttype() {
		return contracttype;
	}

	public void setContracttype(String contracttype) {
		this.contracttype = contracttype;
	}

	public List<?> getContractensure() {
		return contractensure;
	}

	public void setContractensure(List<?> contractensure) {
		this.contractensure = contractensure;
	}

	public String getLoangranttype() {
		return loangranttype;
	}

	public void setLoangranttype(String loangranttype) {
		this.loangranttype = loangranttype;
	}

	public String getGranttime() {
		return granttime;
	}

	public void setGranttime(String granttime) {
		this.granttime = granttime;
	}

	public String getGrantdate() {
		return grantdate;
	}

	public void setGrantdate(String grantdate) {
		this.grantdate = grantdate;
	}

	public String getGrantmoney() {
		return grantmoney;
	}

	public void setGrantmoney(String grantmoney) {
		this.grantmoney = grantmoney;
	}

	public GrantService getGrantService() {
		return grantService;
	}

	public void setGrantService(GrantService grantService) {
		this.grantService = grantService;
	}

	public String getRefunddate() {
		return refunddate;
	}

	public void setRefunddate(String refunddate) {
		this.refunddate = refunddate;
	}

	public ShowContractService getShowContractService() {
		return showContractService;
	}

	public void setShowContractService(ShowContractService showContractService) {
		this.showContractService = showContractService;
	}
}
